#define d3pdrel16_cxx
#include "d3pdrel16.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include "base/particle.h"
#include "Jet.h"
#include "Muon.h"
#include "Electron.h"
#include "TLorentzVector.h"

void d3pdrel16::Loop()
{
//   In a ROOT session, you can do:
//      Root > .L d3pdrel16.C
//      Root > d3pdrel16 t
//      Root > t.GetEntry(12); // Fill t data members with entry number 12
//      Root > t.Show();       // Show values of entry 12
//      Root > t.Show(16);     // Read and show values of entry 16
//      Root > t.Loop();       // Loop on all entries
//

//     This is the loop skeleton where:
//    jentry is the global entry number in the chain
//    ientry is the entry number in the current Tree
//  Note that the argument to GetEntry must be:
//    jentry for TChain::GetEntry
//    ientry for TTree::GetEntry and TBranch::GetEntry
//
//       To read only selected branches, Insert statements like:
// METHOD1:
//    fChain->SetBranchStatus("*",0);  // disable all branches
//    fChain->SetBranchStatus("branchname",1);  // activate branchname
// METHOD2: replace line
//    fChain->GetEntry(jentry);       //read all branches
//by  b_branchname->GetEntry(ientry); //read only this branch
   if (fChain == 0) return;

   Long64_t nentries = fChain->GetEntriesFast();

   Long64_t nbytes = 0, nb = 0;
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
      // if (Cut(ientry) < 0) continue;
   }
}

void d3pdrel16::SwitchBranches(){
	   //Set Ireelevant branches off - Turn on only those of interest
	d3pdrel16::fChain->SetBranchStatus("*",0);
  
	d3pdrel16::fChain->SetBranchStatus("jet_pt",1);
    d3pdrel16::fChain->SetBranchStatus("jet_eta",1);
    d3pdrel16::fChain->SetBranchStatus("jet_phi",1);
    d3pdrel16::fChain->SetBranchStatus("jet_E",1);
	d3pdrel16::fChain->SetBranchStatus("el_px",1);
    d3pdrel16::fChain->SetBranchStatus("el_py",1);
    d3pdrel16::fChain->SetBranchStatus("el_pz",1);
    d3pdrel16::fChain->SetBranchStatus("el_E",1);
    d3pdrel16::fChain->SetBranchStatus("el_charge",1);
        
	d3pdrel16::fChain->SetBranchStatus("mu_px",1);
    d3pdrel16::fChain->SetBranchStatus("mu_py",1);
    d3pdrel16::fChain->SetBranchStatus("mu_pz",1);
    d3pdrel16::fChain->SetBranchStatus("mu_E",1);
    d3pdrel16::fChain->SetBranchStatus("mu_charge",1);
   //endof branch status set
	
}


void d3pdrel16::FillEvtData(unsigned int i){

  if (!fChain){
	  std::cerr<<"Tree Not Initialized ... FAILURE"<<std::endl;
	  exit (8);
	  }

  TLorentzVector lv(0,0,0,0);

fChain->GetEntry(i);

//Fill Jets
try{
	for(unsigned int i=0;i<jet_phi->size();i++){
	
		lv.SetPtEtaPhiE(jet_pt->at(i)/1000.,jet_eta->at(i),
						jet_phi->at(i),jet_E->at(i)/1000.);
		Jet theJet;
		theJet.setP4(lv);
                theJet.setEmFrac(jet_emfrac->at(i));
		jets->push_back(theJet);
	}	
}	
catch(int JetFillFailed){
		std::cerr<<"Filling Jets Failure"<<std::endl;
		exit(8);
}	

//Fill Muons

try{
	
	double c=1000;
	for(unsigned int i=0;i<mu_px->size();i++){
	
		lv.SetPxPyPzE(mu_px->at(i)/c,mu_py->at(i)/c,mu_pz->at(i)/c,mu_E->at(i)/c);
		Muon theMuon;
		theMuon.setP4(lv);
                theMuon.SetCharge(mu_charge->at(i));
		muons->push_back(theMuon);
	}	
}	
catch(int MuonFillFailed ){
		std::cerr<<"Filling Muons Failure"<<std::endl;
		exit(8);
}	

//Fill Electrons
try{
	double c=1000;
	for(unsigned int i=0;i<el_px->size();i++){
	
		lv.SetPxPyPzE(el_px->at(i)/c,el_py->at(i)/c,el_pz->at(i)/c,el_E->at(i)/c);
		Electron theElectron;
		theElectron.setP4(lv);
                theElectron.SetCharge(el_charge->at(i));
		electrons->push_back(theElectron);
	}	
}	
catch(int ElectronFillFailed){
		std::cerr<<"Filling Electrons Failure"<<std::endl;
		exit(8);
}

Ntrks=vxp_n;

}
